package cnn;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class ExecuteCommand {
    public static void main(String[] args) {
        // 要执行的本地命令
//        String exeCommand = "top -s 10";  //-s 20秒刷新一次
        String exeCommand = "killall -9 WeChat";  //-s 20秒刷新一次

        //pmset displaysleepnow 息屏

        // 执行命令
        try {
            Process process = Runtime.getRuntime().exec(exeCommand);

            // 读取命令的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));


            boolean isStart=false;
            String line;
            List<String> cols= new ArrayList<>();
            List<JSONObject> list = new ArrayList<>();
            Map<String, JSONObject> map = new HashMap<>();
            int index;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                if (line.startsWith("Processes:")){
                    continue;
                }
                if (line.startsWith("PID") && isStart==false ){
                    isStart=true;
                    for (String value: line.split(" ")){
                       if (value !=null && value.length()>0){
                           cols.add(value);
                       }
                    }
//                    System.out.println(JSON.toJSON(cols));
                    continue;
                }
                if (isStart){
                    List<String> listData = new ArrayList<>();
                    for (String value: line.split(" ")){
                        if (value !=null && value.length()>0){
                            listData.add(value);
                        }
                    }
                    String command = listData.get(1);
                    JSONObject row = map.get(command);
                    if (row==null){
                        row = new JSONObject();
                    }
                    if (listData.size()<30){
                        System.out.println(JSON.toJSONString(listData));
                        break;
                    }                    row.put("pId",    listData.get(0)); //PID     进程id
                    row.put("command",command);         //COMMAND 线程名
                    row.put("cpu",    listData.get(2)); //%CPU    百分比
                    row.put("mem",    listData.get(7)); //MEM     暂用物理内存大小
                    row.put("state",  listData.get(12));//STATE   运行中还是 休眠
                    row.put("boosts", listData.get(13));//BOOSTS
                    row.put("user",   listData.get(30));//USER


                    if ( !"root".equals(row.getString("USER"))){
                        list.add(row);
                        map.put("COMMAND",row);
                        System.out.println(row.toJSONString());
                    }
                }
            }

            // 等待命令执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
            System.out.println("Exit value: " + exitValue);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}